<?php
 /******************************************************************************
 *   GunCMS is an avid supporter of open source software. This is the appropriate 
 *   option if you are creating an open source application with a license 
 *   compatible with the GNU GPL license v3.
 *   For details http://www.gnu.org/licenses/gpl.html
 *   DO NOT USE FOR COMMERCIAL WITHOUT PERMISSION
 *
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 ********************************************************************************/
class BgcmsTuLCustomerBean {
	
	  public function __construct() {
	  //
	  }
		
	  public function __destruct() {
		//
	  }
	  
	  public static function get_customer_by_id($db, $id) {
    	$strSQL = sprintf("SELECT * FROM %1\$s WHERE customer_id='$id'", TBL_TUL_CUSTOMER);
    	return $db->get_row($strSQL);
    }
    
    public static function save_customer($db, $info) {
    	$strSQL = sprintf("INSERT INTO %s " .
    			"(customer_firstname, customer_lastname, customer_email, " .
    			"customer_password, customer_registered) " .
    			"VALUES ('%s', '%s', '%s', '%s', now())", 
    			TBL_TUL_CUSTOMER,
    			$info['customer_firstname'],
    			$info['customer_lastname'],
    			$info['customer_email'],
          crypt($info['user_password'], '$gcms$'));
    	$res = $db->query($strSQL);
    	if($res == true) {
    		return $db->insert_id;
    	} else
    		return -1;
    }
    
    public static function update_customer($db, $info) {
    	$objCustomer = BgcmsTuLCustomerBean::get_customer_by_id($db, $info['customer_id']);
    	$isChangePass = ($objCustomer->customer_password != $info['customer_password']);
    	
    	$strSQL = sprintf("UPDATE %s SET " .
    			($isChangePass?"customer_password = '%s', ": '%s') .
    			"customer_firstname = '%s', " .
    			"customer_lastname = '%s', " .
    			"customer_email = '%s' " .
    			"WHERE customer_id=" . $info['customer_id'], TBL_TUL_CUSTOMER,
    			($isChangePass?crypt($info['customer_password'], $info['customer_firstname']):''),
    			$info['customer_firstname'],
    			$info['customer_lastname'],
    			$info['customer_email']);	
    	$res = $db->query($strSQL) >= 0;

    	return $res;
    }
    public static function delete_customer_by_id($db, $id) {
    	$strSQL = sprintf("DELETE FROM %1\$s " .
    			"WHERE customer_id='$id'", TBL_TUL_CUSTOMER);
    	$res = $db->query($strSQL) > 0;
    	return $res;
    }
    
    public static function existed_customer_by_email($db, $email) {
    	$strSQL = sprintf("SELECT count(%1\$s.customer_id) FROM %1\$s WHERE customer_email='$email'", TBL_TUL_CUSTOMER);
    	$count = $db->get_var($strSQL);
    	return ($count > 0);
    }
    
    public static function get_all_customer_paging($db,$paging,$path) {
        $page = isset ( $_GET[$path]) ? intval ( $_GET[$path] ) : 1;
        $rows_per_page= $paging;
        $page_start = ( $page - 1 ) * $rows_per_page;
        $page_end = $page * $rows_per_page;
        $strSQL = sprintf("SELECT * FROM %s LIMIT $page_start,$rows_per_page ", TBL_TUL_CUSTOMER);
        return $db->get_results($strSQL);
    }
    
    public static function count_all_customer($db) {
        $strSQL = sprintf("SELECT COUNT(%1\$s.customer_id) as total FROM %1\$s ", TBL_TUL_CUSTOMER);
        $res = $db->get_row($strSQL);
		    return	$res->total;
    }
}
?>    